<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<%@ page import="java.util.*"%>
<%@ page import="cn.myapps.core.validate.repository.action.ValidateRepositoryHelper" %>
<HTML>
<o:MultiLanguage>
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
<META HTTP-EQUIV="pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"> 
<META HTTP-EQUIV="expires" CONTENT="0">
<ww:bean name="cn.myapps.core.dynaform.view.action.ViewHelper" id="viewHelper">
<ww:param name="moduleid" value="#parameters.moduleid"/>
</ww:bean>

<ww:bean name="cn.myapps.core.deploy.module.action.ModuleHelper" id="mh" />
<link href="../css/dialog.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href='<ww:url value="/resource/css/main.css" />' type="text/css">
<script src='<ww:url value="/dwr/engine.js"/>'></script>
<script src='<ww:url value="/dwr/util.js"/>'></script>
<script src='<ww:url value="/dwr/interface/Sequence.js"/>'></script>
<script src='<ww:url value="/dwr/interface/ViewUtil.js"/>'></script>
<script src='<ww:url value="/dwr/interface/ApplicationUtil.js"/>'></script>
<script language=JavaScript src="../../dialog/sequence.js"></script>
<script language=JavaScript src="../../dialog/dialog.js"></script>
<script language=JavaScript src="../../dialog/script.js"></script>
<script src="../../dialog/common/fck_dialog_common.js" type="text/javascript"></script>
<link href="../../css/dialog.css" rel="stylesheet" type="text/css" />

<SCRIPT language=JavaScript>
var dialog	= window.parent ;
var oEditor = dialog.InnerDialogLoaded() ;
// Gets the document DOM
var oDOM = oEditor.FCK.EditorDocument ;
var oActiveEl = dialog.Selection.GetSelectedElement() ;

// 选项卡点击事件
function cardClick(cardID){
	var obj;
	for (var i=1;i<6;i++){
		obj=document.all("card"+i);
		obj.style.backgroundColor="#3A6EA5";
		obj.style.color="#FFFFFF";
	}
	obj=document.all("card"+cardID);
	obj.style.backgroundColor="#FFFFFF";
	obj.style.color="#3A6EA5";

	for (var i=1;i<6;i++){
		obj=document.all("content"+i);
		obj.style.display="none";
	}
	obj=document.all("content"+cardID);
	obj.style.display="";
	resize();
}

//初始值
function InitDocument(){
	var editMode;
	oEditor.FCKLanguageManager.TranslatePage(document) ;
	//ev_init('content.type','_onactionviewid','_onactionformid','_onactionflowid');
	DWREngine.setAsync(false);
	
	if ( oActiveEl)
	{
		if(oActiveEl.name!=null){
			temp.name.value = HTMLDencode(oActiveEl.name);
		}
		
		if(oActiveEl.caption!=null){
	   		temp.caption.value = HTMLDencode(oActiveEl.caption);
		}
		
		temp.refreshOnChanged.checked = oActiveEl.refreshOnChanged == "true";
		
		temp.calculateOnRefresh.checked = oActiveEl.calculateOnRefresh == "true";
		
		if(oActiveEl.mutilSelect == "true"){
			temp.mutilSelect.checked = oActiveEl.mutilSelect == "true";
			temp.selectOne.disabled = true;
		}

		if(oActiveEl.selectOne == "true"){
			temp.selectOne.checked = oActiveEl.selectOne == "true";
			temp.mutilSelect.disabled = true;
		}
		
		temp.allowViewDoc.checked = oActiveEl.allowViewDoc == "true";
		
		
		if(oActiveEl.hiddenScript!=null){
			temp.hiddenScript.value = HTMLDencode(oActiveEl.hiddenScript);
		}

		if(oActiveEl.hiddenValue!=null){
			temp.hiddenValue.value = HTMLDencode(oActiveEl.hiddenValue);
		}

		if(oActiveEl.hiddenPrintScript!=null){
			temp.hiddenPrintScript.value = HTMLDencode(oActiveEl.hiddenPrintScript);
		}

		if(oActiveEl.printHiddenValue!=null){
			temp.printHiddenValue.value = HTMLDencode(oActiveEl.printHiddenValue);
		}
		
		if(oActiveEl.readonlyScript!=null){
			temp.readonlyScript.value = HTMLDencode(oActiveEl.readonlyScript);
		}
		
		if(oActiveEl.okScript!=null){
			temp.okScript.value = HTMLDencode(oActiveEl.okScript);
		}
		
		if(oActiveEl.openType!=null){
			temp.openType.value = HTMLDencode(oActiveEl.openType);
		}
		
		if(oActiveEl.module!=null){
			for(var i=0;i<temp.module.options.length;i++)
				  if(temp.module.options[i].value==HTMLDencode(oActiveEl.module)){
				    temp.module.selectedIndex = i;
					break;
			}
		}
		
		if(oActiveEl.dialogView!=null){
			for(i=0;i<temp.dialogView.options.length;i++)
				if(temp.dialogView.options[i].value==HTMLDencode(oActiveEl.dialogView)){
				    temp.dialogView.selectedIndex = i;
					break;
				} 
		}
		
		if(oActiveEl.validateLibs!=null){
			var libstr=HTMLDencode(oActiveEl.validateLibs);
			var lib=libstr.split(';');
			var validateLibs=document.getElementsByName('validateLibs');
			for(i=0; i< lib.length; i++){
			     for(var j=0; j< validateLibs.length; j++){
		   			if(validateLibs[j].value==lib[i]){
		   				validateLibs[j].checked=true;
		   			}
		   		}
		   	}
		}

		if(oActiveEl.mapping!=null){
			var mappStr = HTMLDencode(oActiveEl.mapping);
			var datas = parseMappStr(mappStr);
			addViewOptions(oActiveEl.module, oActiveEl.dialogView, datas);
		}
	}
	else {
		addMappRows();
		oActiveEl = null ;
	}
		
	dialog.SetOkButton( true ) ;
	//dialog.SetAutoSize( true ) ;
	SelectField( 'name' ) ;
	window.top.toThisHelpPage("application_module_form_info_advance_viewdialog");
}

function Ok()
{
	if(ev_check()){
	oEditor.FCKUndo.SaveUndoStep() ;
	var className="cn.myapps.core.dynaform.form.ejb.ViewDialogField";
	var id=getFieldId();	
	var validateLibs=document.getElementsByName('validateLibs');
	var libs='';
	for(var i=0; i< validateLibs.length; i++){
 		if(validateLibs[i].checked){
 			libs+=validateLibs[i].value+";";
 		}
 	}
 	libs=libs.substring(0,libs.length-1);
	oActiveEl = CreateNamedElement( oEditor, oActiveEl, 'img', {
									classname: className,
									src:"images/viewdialog.gif",
									id: id,
									type:"viewdialogfield",
									name:HTMLEncode(temp.name.value),
									caption:HTMLEncode(temp.caption.value),
									refreshOnChanged:HTMLEncode(temp.refreshOnChanged.checked+""),
									calculateOnRefresh:HTMLEncode(temp.calculateOnRefresh.checked+""),
									mutilSelect: HTMLEncode(temp.mutilSelect.checked+""),
									selectOne: HTMLEncode(temp.selectOne.checked+""),
									allowViewDoc: HTMLEncode(temp.allowViewDoc.checked+""),
									openType:HTMLEncode(temp.openType.value),
									hiddenScript:HTMLEncode(temp.hiddenScript.value),
									hiddenValue:HTMLEncode(temp.hiddenValue.value),//////
									hiddenPrintScript:HTMLEncode(temp.hiddenPrintScript.value),
									printHiddenValue:HTMLEncode(temp.printHiddenValue.value),
									readonlyScript:HTMLEncode(temp.readonlyScript.value),
									okScript:HTMLEncode(temp.okScript.value),
									validateLibs:HTMLEncode(libs),
									dialogView:HTMLEncode(temp.dialogView.options[temp.dialogView.selectedIndex].value),
									module:HTMLEncode(temp.module.options[temp.module.selectedIndex].value),
									mapping:HTMLEncode(createMappStr())
									} 
								   ) ;	
	return true ;
	}
}
//检查内容是否完成正确
function ev_check(){
   if(temp.name.value==''){
 	 alert("{*[page.name.notexist]*}");
	  return false;
   }else if(temp.module.selectedIndex==0){
	 	 alert("{*[please.choose.module]*}");
		  return false;
   }else if(temp.dialogView.selectedIndex==0){
	 	 alert("{*[page.resource.view.notchoose]*}");
		  return false;
  }else if(createMappStr()==''){
	   alert("{*[core.dts.exp.columnmapping.field]*}");
       return false;
  }
   return !checkStartChar(temp.name.value);
}

function checkStartChar(value){
	return IsDigit(value,"{*[page.name.startingit]*}");
}

var instance = '<%= request.getParameter("application")%>';
// 添加dialog view元素的options
function addViewOptions(modId, defValues, datas) {
	ApplicationUtil.getViewByMod(modId,instance, function(map) {
		var elem = document.getElementById('dv');
		DWRUtil.removeAllOptions(elem.id);
		DWRUtil.addOptions(elem.id, map);
		addAllColOptions('');
		
		if (defValues) {
			DWRUtil.setValue(elem.id, defValues);
			addMappRows(datas);
		}
	});
}

// 添加所有colname元素的options
function addAllColOptions(viewId, defValues) {
	ViewUtil.getColsByView(viewId, function(map) {
		var colSels = document.getElementsByName("colname");
		for(var i=0;i<colSels.length;i++) {
			DWRUtil.removeAllOptions(colSels[i].id);
			DWRUtil.addOptions(colSels[i].id, map);
			if (defValues) { // 设置column name 默认选项
				DWRUtil.setValue(colSels[i].id, defValues[i]);
			}
		}
	});
}

var rowIndex = 2;
var getFrom = function(data) { return '{*[From]*}:' };
var getColName = function(data) { 
	var s =''; 
	s +='<select id="colId'+ rowIndex +'" name="colname" value="' + data.colname + '">';
	s +='<option value="">{*[Select]*}</option>';	
	s +='</select>';
	return s; 
}; 
var getTo = function(data) { return '{*[To]*}:' };
var getFldName = function(data) {
  	return '<input type="text" name="fldname" value="'+ data.fldname + '"/>';
};
var getDelete = function(data) {
  	var s = '<input type="button" value="{*[Delete]*}" onclick="delMappRow(this.parentNode.parentNode)"/>'
  	rowIndex ++;
  	return s;
};
//DWRUtil.setEscapeHtml(false);

// 根据数据增加行
function addMappRows(datas) {
	var cellFuncs = [getFrom, getColName, getTo, getFldName, getDelete];

	var rowdatas = datas;
	
	if (!datas) {
		var data = {colname:'', fldname:''};
		rowdatas = [data];
	}
	
	DWRUtil.addRows("mapping", rowdatas, cellFuncs);
	if (document.getElementsByName("dialogView")[0].value != '') {
		var defValues = new Array();
		
		if (datas) { //设置默认值
			for(var i=0;i<datas.length;i++) {
				defValues[i] = datas[i].colname;
			}
		} else {
			var colSels = document.getElementsByName("colname");
			for(var i=0;i<colSels.length;i++) {
				defValues[i] = colSels[i].value;
			}
		}
		addAllColOptions(document.getElementsByName("dialogView")[0].value, defValues);
	}
	resize();
}
// 删除一行
function delMappRow(row) {
	mapping.deleteRow(row.rowIndex);
	rowIndex --;
	resize();
}
// 根据mapping str获取datas array
function parseMappStr(str) {
	var mapps = str.split(";");
	var array = new Array();
	array[0] = {colname:'', fldname:''};
	for (var i=0;i<mapps.length;i++) {
		var rel = mapps[i].split(":");
		var colValue = rel[0];
		var fldValue = rel[1];	
		var map = {colname : colValue, fldname : fldValue};
		array[i] = map;
	}
	return array;
}

// 根据页面内容生成mapping string
function createMappStr() {
	var cols = document.getElementsByName("colname");
	var flds = document.getElementsByName("fldname");
	var mappStr = '';
	for (var i=0;i<cols.length;i++) {
		if (cols[i].value != '' && flds[i].value != '') {
			mappStr += HTMLEncode(cols[i].value + ":" + flds[i].value) +";";
		}
	}
mappStr = mappStr.substring(0, mappStr.length - 1);
	return  mappStr;
}
function IsDigit(s,msg){
	var text = s.substring(0,1);
	var patrn=/^[0-9]{1}$/; 
	if(s=="action"){
	   	alert("action为关键字，不能作为字段名！");
	   	return true;
   	}else if (patrn.exec(text)){
		alert(msg);
		return true;
	}
	return false;
} 

function changeChecked(){
	var _selectOne = document.getElementById("selectOne");
	var _mutilSelect = document.getElementById("mutilSelect");
	if(_selectOne.checked){
		_mutilSelect.checked = false;
		_mutilSelect.disabled = true;
		_selectOne.disable = false;
	}else if(_mutilSelect.checked){
		_selectOne.checked = false;
		_selectOne.disabled = true;
		_mutilSelect.disable = false;
	}else{
		_mutilSelect.disabled = false;
		_selectOne.disabled = false;
	}
		
}
</script>
 
</HEAD>

<BODY onload="InitDocument()">

<form name="temp">
<table border=0 cellpadding=3 cellspacing=0 width="520px">
<tr align=center>
	<td class="card" onclick="cardClick(1)" id="card1">{*[Basic]*}</td>
	<td width=2></td>
	<td class="card" onclick="cardClick(2)" id="card2">{*[Hidden_Script]*}</td>
	<td width=2></td>
	<td class="card" onclick="cardClick(3)" id="card3">{*[Hidden_Print_Script]*}</td>
	<td width=2></td>
	<td class="card" onclick="cardClick(4)" id="card4">{*[ReadOnly_Script]*}</td>
    <td width=2></td>
     <td class="card" onclick="cardClick(5)" id="card5">{*[Ok_Script]*}</td>
    <td width=2></td>
</tr>

<tr>
	<td bgcolor=#ffffff align=center valign=middle colspan=13  width="100%">
<!-- *************************** content1 ********************************* -->
	<table border=1 cellpadding=3 cellspacing=1 class="content" id="content1" width="520px">
	<tr>
		<td class="commFont commLabel">{*[Name]*}:</td>
		<td><input type=text name="name" size="40" onchange="checkStartChar(this.value);"></td>
	</tr>
	<tr>
		<td class="commFont commLabel">{*[Caption]*}:</td>
		<td><input type=text name="caption" size="40"></td>
	</tr>	
	<tr>
		<td class="commFont commLabel">{*[Module]*}:</td>
		<td>
			<ww:select name="module" list="#mh.getModuleSel(#parameters.application)" 
			theme="simple" cssStyle="width:275" onchange="addViewOptions(this.value)"/>
		</td>
	</tr>
	
	<tr>
		<td class="commFont commLabel">{*[View]*}:</td>
		<td> 
			<ww:select id="dv" name="dialogView" list="#{'':'{*[Select]*}'}" theme="simple" cssStyle="width:275" 
			onchange="addAllColOptions(this.value)"/>
		</td>
		</tr>
		<tr>
		<td class="commFont commLabel">{*[Open_Type]*}:</td>
		<td>
		    <input id="openType" name="openType" value="3" type="hidden"/>
		    {*[Open.in.working.div]*} 
			<!-- <select id="openType" name="openType">
				<option value="3">{*[Open.in.working.div]*}</option>
		    	<option value="1">{*[Open.in.pop.window]*}</option>
		    </select>-->
		</td>
	</tr>
	<tr>
		<td colspan="2" align="center">
			<input type=checkbox name="refreshOnChanged" />{*[Refresh]*}&nbsp;
			<input type=checkbox name="calculateOnRefresh" />{*[Recalculate]*}&nbsp;
			<input type=checkbox name="selectOne" id="selectOne" onclick="changeChecked()"/>{*[Single]*}{*[Choose]*}&nbsp;
			<input type=checkbox name="mutilSelect" id="mutilSelect" onclick="changeChecked()"/>{*[Multi_Select]*}&nbsp;
			<input type=checkbox name="allowViewDoc" />{*[AllowViewDoc]*}
		</td>
	</tr>
	
	<tr>
		<td class="commFont commLabel">
			{*[Mapping]*}:
		</td>
		<td>
		<table border="0" width="100%">
			<tbody id="mapping"> 
			<tr><td colspan="5" align="right">
				<input type="button" value="{*[Add]*}" onclick="addMappRows()"/> 
			</td></tr>
			<tr>
				<th width="15%"></th>
				<th>{*[ColumnName]*}</th>
				<th width="10%"></th>
				<th>{*[FieldName]*}</th>
				<th width="25%"></th>
			</tr>
			</tbody>
		</table>
		</td>
	</tr>
	</table>
<!-- *************************** content2 ********************************* -->		
<!-- *************************** content3 ********************************* -->	
<!-- *************************** content4 ********************************* -->	
	<table border=1 cellpadding=3 cellspacing=1 class="content" id="content2" width="520px">
	<tr>
		<td><textarea name="hiddenScript" style="width:96%"  rows="10"></textarea>
		<button style="border:0px;cursor: pointer;" onclick="openIscriptEditor('hiddenScript','{*[Script]*}{*[Editor]*}','{*[Hidden_Script]*}','name','{*[Save]*}{*[Success]*}');"><img alt="Open with IscriptEditor" src="<ww:url value='/resource/image/editor.png' />"/></button>
		</td>
	</tr>
	<tr>
		<td>{*[hiddenValue]*}：<input id="hiddenValue" type="text" name="hiddenValue" /></td>
	</tr>
	</table>
	<table border=1 cellpadding=3 cellspacing=1 class="content" id="content3" >
	<tr>
		<td><textarea name="hiddenPrintScript" style="width:92%"  rows="6"></textarea>
		<button style="border:0px;cursor: pointer;" onclick="openIscriptEditor('hiddenPrintScript','{*[Script]*}{*[Editor]*}','{*[Hidden_Print_Script]*}','name','{*[Save]*}{*[Success]*}');"><img alt="Open with IscriptEditor" src="<ww:url value='/resource/image/editor.png' />"/></button>
		</td>
	</tr>
	<tr>
		<td>{*[Print]*}{*[hiddenValue]*}：<input  type="text" name="printHiddenValue" /></td>
	</tr>
	</table>
<!-- *************************** content6 ********************************* -->		
	<table border=1 cellpadding=3 cellspacing=1 class="content" id="content4" width="520px">
	<tr>
		<td><textarea name="readonlyScript" style="width:96%" rows="10"></textarea>
		<button style="border:0px;cursor: pointer;" onclick="openIscriptEditor('readonlyScript','{*[Script]*}{*[Editor]*}','{*[ReadOnly_Script]*}','name','{*[Save]*}{*[Success]*}');"><img alt="Open with IscriptEditor" src="<ww:url value='/resource/image/editor.png' />"/></button>
		
		</td>
	</tr>
	</table>
<!--****************************  content7   ************************************ -->
     <table border=1 cellpadding=3 cellspacing=1 class="content" id="content5" width="520px" >
	<tr>
		<td><textarea name="okScript" style="width:96%" rows="10" ></textarea>
		<button style="border:0px;cursor: pointer;" onclick="openIscriptEditor('okScript','{*[Script]*}{*[Editor]*}','{*[Ok_Script]*}','name','{*[Save]*}{*[Success]*}');"><img alt="Open with IscriptEditor" src="<ww:url value='/resource/image/editor.png' />"/></button>
		
		
		</td>
	</tr>
	</table>

	</td>
</tr>
</table>
</form>

<script language=javascript>
cardClick(1);

</script>

</BODY>
</o:MultiLanguage>
</HTML>
